package mammuthus.controller.app

import net.csdn.annotation.rest.{State, BasicInfo, At}
import net.csdn.modules.http.{ApplicationController, ViewType, RestRequest}
import net.liftweb.{json => SJSon}
import mammuthus.tool.Constants
import com.google.inject.Inject
import mammuthus.master.Master
import net.csdn.common.env.Environment
import mammuthus.protocal.WriteFileTaskResponse
import mammuthus.controller.filter.{TokenAuthFilter, AppNameCheckFilter}

/**
  * 6/9/15 WilliamZhu(allwefantasy@gmail.com)
  */
class AppDiscoveryTaskController @Inject()(akkaMaster: Master, env: Environment) extends ApplicationController with TokenAuthFilter with AppNameCheckFilter{

   implicit val formats = SJSon.Serialization.formats(SJSon.NoTypeHints)

   val appRootDir = Constants.getAppRootDir

   def appName = param("appName")

  @At(path = Array("/app/register"), types = Array(RestRequest.Method.GET, RestRequest.Method.POST))
  @BasicInfo(
    desc = "",
    state = State.alpha,
    testParams = "?appName=zookeeper&hostName=&module=master&clientPort=2181",
    testResult = "",
    author = "WilliamZhu",
    email = "allwefantasy@gmail.com"
  )
  def register = {
    if (isEmpty(param("appName")) ||
      isEmpty(param("hostName")) ||
      isEmpty(param("module")) ||
      isEmpty(param("clientPort"))
    ) render(400, s"appName,hostName,module,clientPort required ")
    val path = Constants.appInfoRootDir + s"${param("appName")}"
    val res = akkaMaster.writeJSONFile(param("hostName"), Map(
      "app_module_" + param("module") -> s"${param("hostName")}:${param("clientPort")}"
    ), path, true)
    render(SJSon.Serialization.write(res), ViewType.json)
  }

  @At(path = Array("/app/get/register"), types = Array(RestRequest.Method.GET, RestRequest.Method.POST))
  @BasicInfo(
    desc = "",
    state = State.alpha,
    testParams = "?appName=zookeeper&module=master",
    testResult = "",
    author = "WilliamZhu",
    email = "allwefantasy@gmail.com"
  )
  def getAppRegister = {
    if (isEmpty(param("appName")) ||
      isEmpty(param("module"))
    ) render(400, s"appName,module required")
    val res = akkaMaster.findHostAndPortByAppName(param("appName"),param("module"))

    if (!isEmpty(param("split"))) {
      render(res.mkString(param("split")), ViewType.string)
    } else {
      render(SJSon.Serialization.write(res), ViewType.json)
    }

  }

  @At(path = Array("/app/remove/register"), types = Array(RestRequest.Method.GET, RestRequest.Method.POST))
  @BasicInfo(
    desc = "",
    state = State.alpha,
    testParams = "?appName=zookeeper&module=master",
    testResult = "",
    author = "WilliamZhu",
    email = "allwefantasy@gmail.com"
  )
  def removeAppRegister = {
    if (isEmpty(param("appName")) ||
      isEmpty(param("module"))
    ) render(400, s"appName,module required")

    val items = new scala.collection.mutable.ArrayBuffer[WriteFileTaskResponse]()

    akkaMaster.findSlaveInfoByAppName(param("appName")).foreach {
      f =>
        f._2._2.appInfo.map {
          appInfoPerHost =>
            if (appInfoPerHost.appName == param("appName")) {
              val newParams = appInfoPerHost.params - s"app_module_${param("module")}"
              items ++= akkaMaster.writeJSONFile(f._1, newParams, Constants.appInfoRootDir + s"${param("appName")}", false)
            }
        }
    }
    render(SJSon.Serialization.write(items), ViewType.json)

  }

 }
